package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.constant.WareConstant;
import com.shop.mapper.Wms_Purchase_Detail_Mapper;
import com.shop.mapper.Wms_Purchase_Mapper;
import com.shop.pojo.ums.Ums_Member;
import com.shop.pojo.wms.Wms_Purchase;

import com.shop.pojo.wms.Wms_Purchase_Detail;
import com.shop.service.Wms_Purchase_Detail_Service;

import com.shop.pojo.wms.Wms_Ware_Info;

import com.shop.service.Wms_Purchase_Service;
import com.shop.utils.CaiGouVo;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 采购单—李贝贝
 */
@Service
public class Wms_Purchase_ServiceImpl extends ServiceImpl<Wms_Purchase_Mapper,Wms_Purchase> implements Wms_Purchase_Service {

    @Autowired
    private Wms_Purchase_Mapper wms_purchase_mapper;

    @Autowired
    private Wms_Purchase_Detail_Service wms_purchase_detail_service;
    //注入采购单
    @Autowired
    private Wms_Purchase_Detail_Mapper wms_purchase_detail_mapper;

    /**
     * 后台合并所有采购单
     * @param caiGouVo
     * @return
     */
    @Transactional
    @Override
    public R CaiGou_admin_HeBIn_All(CaiGouVo caiGouVo) {
        //首先获取是否选择采购单
        Long purchaseId = caiGouVo.getPurchase();
        Long ware_id = null;

        //判断如果没有选择采购单的话就选择创建一个新的饿采购单
        if(purchaseId == null){
            Wms_Purchase wp = new Wms_Purchase();
            wp.setStatus(WareConstant.PurchaseStatusEnum.CREATE.getCode());//获取常量状态
            wp.setCreate_time(new Date());
            wp.setUpdate_time(new Date());
            wp.setWare_id((long) 1);
            boolean save = this.save(wp);//调用当的添加方法
            purchaseId = wp.getId();//获取自动创建采购单的ID
            ware_id = wp.getWare_id();
            System.out.println("新建的合并单拿到的ID"+""+purchaseId);
        }

        List<Long> item = caiGouVo.getId();
        Long finalPurchaseId = purchaseId;//获取自动创建采购单的ID
        System.out.println(finalPurchaseId);
        Long finalWare_id = ware_id;//获取自动创建仓库ID
        List<Wms_Purchase_Detail> collect = item.stream().map(i -> {
            Wms_Purchase_Detail wpd = new Wms_Purchase_Detail();
            wpd.setId(i);
            wpd.setPurchase_id(finalPurchaseId);
            wpd.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSINGED.getCode()); //获取常量状态进行已分配修改
            wpd.setWare_id(finalWare_id);
            return wpd;
        }).collect(Collectors.toList());

        boolean b = wms_purchase_detail_service.updateBatchById(collect);

        Wms_Purchase wps = new Wms_Purchase();
        wps.setId(purchaseId);
        wps.setUpdate_time(new Date());//修改最后的更新时间
        this.updateById(wps);
        if(b == true){
            return R.success();
        }
        return R.error(500,"合并失败");
    }


    /**
     * 显示所有采购单
     * @param pages
     * @param wms_purchase
     * @return
     */
    @Override
    public LayuiUtils<Wms_Purchase> find_all_pruchase(Page<Wms_Purchase> pages, Wms_Purchase wms_purchase) {
       LayuiUtils<Wms_Purchase> layuis = new LayuiUtils<>();
        QueryWrapper<Wms_Purchase> query = new QueryWrapper<>();
        if(wms_purchase.getWare_id() != null){
            query.eq("ware_id",wms_purchase.getWare_id());
        }

        if(wms_purchase.getStatus() != null){
            query.eq("status",wms_purchase.getStatus());
        }


        Page<Wms_Purchase> wms_purchasePage = wms_purchase_mapper.selectPage(pages,query);
        layuis.setCount(wms_purchasePage.getTotal());
        layuis.setData(wms_purchasePage.getRecords());
        return layuis;
    }

    /**
     * 添加采购单
     * @param wms_purchase
     * @return
     */
    @Override
    public R insert_purchase(Wms_Purchase wms_purchase) {
        wms_purchase.setCreate_time(new Date());
        int insert = wms_purchase_mapper.insert(wms_purchase);
        if (insert > 0){
            return R.success();
        }
        return R.error(500,"添加失败");
    }

    /**
     * 删除采购单
     * @param id
     * @return
     */
    @Override
    public R delete_purchase(List<Long> id) {
        int ids = wms_purchase_mapper.deleteBatchIds(id);
        if (ids > 0){
            return R.success();
        }else {
            return R.error(500,"删除失败");
        }
    }

    /**
     * 修改采购单
     * @param wms_purchase
     * @return
     */
    @Override
    public R update_purchase(Wms_Purchase wms_purchase) {
        wms_purchase.setUpdate_time(new Date());
        //判断如果已分配给采购人的话 就把状态修改为已分配状态
        if(wms_purchase.getAssignee_id() != null){
            wms_purchase.setStatus(WareConstant.PurchaseStatusEnum.ASSINGED.getCode());
        }
        int o = wms_purchase_mapper.updateById(wms_purchase);
        if (o > 0){
            return R.success();
        }else {
            return R.error(500,"修改失败");
        }
    }

    /**
     * 获取用户 用来分配采购单
     * @return
     */

}
